package com.baymax.test.scene.demo;

import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bytedance.scene.Scene;

public class VerticalCenter extends LayoutManagerScene.RVCase {

    public static final int REQUEST_CHILD_ON_SCREEN_TYPE_ANDROID = 1;
    public static final int REQUEST_CHILD_ON_SCREEN_TYPE_CENTER = 0;
    public static final int REQUEST_CHILD_ON_SCREEN_TYPE_NONE = 2;

    public VerticalCenter(@NonNull Scene scene, @NonNull ViewGroup parent, int width, int height) {
        super(scene, parent, width, height);
        getRv().setLayoutManager(new LinearLayoutManager(scene.getSceneContext(), LinearLayoutManager.VERTICAL, false) {

            private int type = 0;
            int scrollOffset = 0;
            int threshold = 10;

            @Override
            public boolean requestChildRectangleOnScreen(@NonNull RecyclerView parent, @NonNull View child, @NonNull Rect rect, boolean immediate, boolean focusedChildVisible) {
                int paddingTop = parent.getPaddingTop();
                int yOffset = child.getTop() + rect.top - child.getScrollY();
                int contentViewHeight = parent.getHeight() - parent.getPaddingBottom();
                int yEdge = rect.bottom + yOffset;
                boolean isChildVisible = yOffset > paddingTop && yEdge < contentViewHeight;
                Log.d("ChildOnScreenScroller", "requestChildRectangleOnScreen isChildVisible:" + isChildVisible);
                int i6 = (int) (paddingTop + parent.getHeight() * 0.5F);

                View findFocus = child;
                if (findFocus == null) {
                    findFocus = parent.findFocus();
                }

                if (type == REQUEST_CHILD_ON_SCREEN_TYPE_CENTER) {
                    int scrollY = (int) (yOffset + findFocus.getHeight() * 0.5F) - i6 + this.scrollOffset;

                    if (Math.abs(scrollY) < threshold) {
                        return true;
                    }
                    exeScrollRecyclerView(parent, child, 0, scrollY, immediate);
                    return (scrollY != 0);
                }

                int top = child.getTop();
                if (top != 0) {
                    int scrollY = top + child.getHeight() - parent.getHeight();
                    exeScrollRecyclerView(parent, child, 0, scrollY, immediate);
                }
                return true;
            }

            private void exeScrollRecyclerView(RecyclerView recycleView, View child, int x, int y, boolean immediate) {
                if (immediate) {
                    recycleView.scrollBy(x, y);
                    return;
                }
                recycleView.smoothScrollBy(x, y);
            }
        });
    }


    @Override
    public void plug() {
        getRv().setAdapter(newAdapter(10));
    }
}
